var express = require('express');
var axios = require('axios');
const multer = require('multer')
var router = express.Router();
require('url-search-params-polyfill');


var examNum = 0
var Works = new Array()
var testWordIds
var examWordId
var promates
var promatesIndex = 0

/**
 * @api {get} /admin/addpage 渲染页面
 * @apiName 渲染页面
 * @apiGroup page
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    // var testWordId = req.params.id;
    var { testWordId, id } = req.query

    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/ExamWork/forAddSubject',
        params: {
            examWordId: id
        },
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("获取试卷信息成功")
                // examNum = response.data.data.promates.length
                // promates = response.data.data.promates
                res.render('admin/addpage', {
                    list: response.data.data
                });
                res.send({ err: 0, msg: "获取试卷信息成功", data: response.data.data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取试卷信息失败" })
                console.log("获取试卷信息失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误，请刷新页面" })
            console.log("服务器错误")
        });
})

/**
 * @api {get} /admin/addpage/update 回显出题页面
 * @apiName 回显出题页面
 * @apiGroup page
 *
 * @apiParam {String} id 题目id.
 * 
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/update',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    let { id } = req.query
    console.log(id)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Subject/getSubjectsById',
        params: {
            id: id,
            Id: id
        },
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("获取试卷信息成功")
                res.render('admin/updatepage', {
                    list: response.data.data
                });
                res.send({ err: 0, msg: "获取试卷信息成功", data: response.data.data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取试卷信息失败" })
                console.log("获取试卷信息失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误，请刷新页面" })
            console.log("服务器错误")
        });
})

/**
 * @api {post} /admin/addpage/addexamworks 上传题目
 * @apiName 发布页面
 * @apiGroup page
 *
 * @apiParam {String} answerA A选项.
 * @apiParam {String} answerB B选项.
 * @apiParam {String} answerC C选项.
 * @apiParam {String} answerD D选项.
 * @apiParam {String} picUrl 图片地址.
 * @apiParam {String} relAnswer 正确答案.
 * @apiParam {String} isMany 是否多选.
 * @apiParam {String} subjectValue 分值.
 * @apiParam {String} remark 备注.
 * @apiParam {String} subjectNumber 题目编号.
 * @apiParam {String} testWordId 试卷number.
 * @apiParam {String} examWordId 出题任务id.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/addexamworks',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, (req, res, next) => {
    console.log(req.body)
    let { answerA, answerB, answerC, answerD, picUrl, isMany, relAnswer, subjectValue, remark, subjectNumber, testWordId, examWordId } = req.body

    var param = new URLSearchParams()
    param.append('answerA', answerA)
    param.append('answerB', answerB)
    param.append('answerC', answerC)
    param.append('answerD', answerD)
    param.append('picUrl', picUrl)
    param.append('relAnswer', relAnswer)
    param.append('isMany', isMany)
    param.append('testWordId', testWordId)
    param.append('examWordId', examWordId)
    param.append('subjectValue', subjectValue)
    param.append('remark', remark)
    param.append('subjectNumber', subjectNumber)
    

    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Subject/addSubject',
        data: param,
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("试题提交成功")
                if (response.data.data == 0) {
                    res.send({ err: 0, msg: "此出题任务已全部完成，", data: { url: "/admin/addpagelist" } })
                } else {
                    res.send({ err: 0, msg: "本题已完成", data: { url: "/admin/addpage?testWordId=" + testWordId + "&id=" + examWordId } })
                }
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "试题提交错误，请重新上传" })
                console.log("试题提交错误")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误，请刷新页面" })
            console.log("提交错误，服务器错误")
        });

})

/**
 * @api {post} /admin/addpage/updateexamworks 更新修改题目
 * @apiName 更新修改题目
 * @apiGroup page
 *
 * @apiParam {String} id 题目id.
 * @apiParam {String} answerA A选项.
 * @apiParam {String} answerB B选项.
 * @apiParam {String} answerC C选项.
 * @apiParam {String} answerD D选项.
 * @apiParam {String} picUrl 图片地址.
 * @apiParam {String} relAnswer 正确答案.
 * @apiParam {String} isMany 是否多选.
 * @apiParam {String} subjectValue 分值.
 * @apiParam {String} remark 备注.
 * @apiParam {String} subjectNumber 题目编号.
 * @apiParam {String} testWordId 试卷number.
 * @apiParam {String} examWordId 出题任务id.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/updateexamworks',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, (req, res, next) => {
    console.log(req.body)
    let { id, answerA, answerB, answerC, answerD, picUrl, isMany, relAnswer, subjectValue, remark, subjectNumber, testWordId, examWordId } = req.body


    var param = new URLSearchParams()
    param.append('id', id)
    param.append('answerA', answerA)
    param.append('answerB', answerB)
    param.append('answerC', answerC)
    param.append('answerD', answerD)
    param.append('picUrl', picUrl)
    param.append('relAnswer', relAnswer)
    param.append('isMany', isMany)
    param.append('testWordId', testWordId)
    param.append('examWordId', examWordId)
    param.append('subjectValue', subjectValue)
    param.append('remark', remark)
    param.append('subjectNumber', subjectNumber)
    // Works.push(work)

    // Works = JSON.stringify(Works)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Subject/updateSubjectById',
        data: param,
    }
    axios(config)
        .then(function (response) {
            // console.log(response.data);
            if (response.data.code == 0) {
                console.log("修改成功")
                res.send({ err: 0, msg: "修改成功", data: { url: "/admin/endpagelist" } })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "修改失败", data: { url: "/admin/endpagelist" } })
                console.log("修改失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误，请刷新页面" })
            console.log("更新失败，服务器错误")
        });
})


module.exports = router;
